package com.ittsang.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittsang.dto.CommentDTO;
import com.ittsang.dto.PageDTO;
import com.ittsang.dto.R;
import com.ittsang.dto.UserDTO;
import com.ittsang.mapper.ArticleCommentMapper;
import com.ittsang.mapper.UserMapper;
import com.ittsang.pojo.Article;
import com.ittsang.pojo.ArticleComment;
import com.ittsang.service.CommentService;
import com.ittsang.utils.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private ArticleCommentMapper articleCommentMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public R getCommentByArticle(PageDTO pageDTO) {

        // 分页操作
        int pageNum = pageDTO.getPageNum();
        int pageSize = pageDTO.getPagesize();
        PageHelper.startPage(pageNum,pageSize);
        int skip= (pageNum-1)*pageSize;

        UserDTO user = UserHolder.getUser();

        List<ArticleComment> articleComments = articleCommentMapper.getCommentByArticle(pageDTO.getArticleId());
        for (ArticleComment articleComment : articleComments) {
            articleComment.setName(userMapper.getUserName(articleComment.getUserId()));
            articleComment.setPhote(userMapper.getUserPhote(articleComment.getUserId()));
            if (user != null) {
                if (Math.toIntExact(user.getId()) == articleComment.getUserId()) {
                    articleComment.setUserStatus(1);
                }
            }
        }
        Page<ArticleComment> page = new Page<>(pageNum, pageSize);
        page.setTotal(articleComments.size());
        List<ArticleComment> articleCommentList = articleComments.stream().skip(skip)
                .limit(pageSize).collect(Collectors.toList());
        page.addAll(articleCommentList);

        return R.ok("查询成功").put("articleCommentList", page);
    }


    @Override
    public R insertComment(CommentDTO commentDTO) {
        UserDTO user = UserHolder.getUser();

        if (user == null) {
            return R.error("未登录");
        }

        int num = articleCommentMapper.insertComment(commentDTO.getComment(), user.getId(), commentDTO.getArticleId());

        return num > 0 ? R.ok("添加成功") : R.error("添加失败");
    }


    @Override
    public R deletedComment(Integer commentId) {
        return articleCommentMapper.deletedComment(commentId) > 0 ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R getUserAllComment(PageDTO pageDTO) {
        // 分页操作
        int pageNum = pageDTO.getPageNum();
        int pageSize = pageDTO.getPagesize();
        PageHelper.startPage(pageNum,pageSize);
        int skip= (pageNum-1)*pageSize;

        List<ArticleComment> articleComments = articleCommentMapper.getUserAllComment(UserHolder.getUser().getId());
        for (ArticleComment articleComment : articleComments) {
            articleComment.setUserStatus(1);
        }
        Page<ArticleComment> page = new Page<>(pageNum, pageSize);
        page.setTotal(articleComments.size());
        List<ArticleComment> articleCommentList = articleComments.stream().skip(skip)
                .limit(pageSize).collect(Collectors.toList());
        page.addAll(articleCommentList);
        return R.ok("获取成功").put("articleComments", page);
    }
}
